package com.qdlc.p2p.common.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 * 工具类-随机数
 * 
 * @author xx
 * @version 1.0
 */
public class RandomUtil {

	/**
	 * 获取从a至z，长度为length随机数
	 * 
	 * @return
	 */
	public static String getRandomStr(int length) {
		String base = "abcdefghijklmnopqrstuvwxyz";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * 获取范围内int值
	 * 
	 * @param 获取范围内int值
	 * @return
	 */
	public static int getRandomRange(int max, int min) {
		return (int) (Math.random() * (max - min) + min);
	}

	/**
	 * 获取随机长度随机字符
	 * 
	 * @param length base
	 * @return
	 */
	public static String getRandomString(int length, String base) { // length表示生成字符串的长度
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * 获取随机长度随机字符
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomString(int length) { // length表示生成字符串的长度
		String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * 获取随机长度随机数字
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomNumString(int length) { // length表示生成字符串的长度
		String base = "0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * 返回随机数组
	 * 
	 * @param start 开始值
	 * @param end 结束值
	 * @return
	 */
	public static int[] getRangRandom(int start, int end) {
		return getRangRandom(start, end, end - start + 1);
	}

	/**
	 * 返回指定范围指定个数的不重复随机数。
	 * 
	 * @param start
	 * @param end
	 * @param num
	 * @return
	 */
	public static int[] getRangRandom(int start, int end, int num) {

		int length = end - start + 1;
		// 参数不合法
		if (length < 1 || num > length) {
			return null;
		} else {
			int[] numbers = new int[length];
			int[] result = new int[num];
			// 循环赋初始值
			for (int i = 0; i < length; i++) {
				numbers[i] = i + start;
			}
			Random random = new Random();
			// 取randomMax次数
			for (int i = 0; i < num; i++) {
				// 随机获取取数的位置
				int m = random.nextInt(length - i) + i;
				result[i] = numbers[m];
				// 交换位置
				int temp = numbers[m];
				numbers[m] = numbers[i];
				numbers[i] = temp;
			}
			return result;
		}
	}

	/*
	 * 生成6位随机数验证码
	 */
	public static String code() {
		List<Integer> list = GetRandomNumber();
		Iterator<Integer> iterator = list.iterator();
		String temp = "";
		while (iterator.hasNext()) {
			temp += iterator.next();
		}
		return temp;
	}

	public static List<Integer> GetRandomNumber() {
		List<Integer> list = new ArrayList<Integer>();
		Random random = new Random();
		while (list.size() < 6) {
			int randomInt = random.nextInt(10);
			if(list.contains(randomInt)){
				continue;
			}
			else{
				list.add(randomInt);
			}
		}
		return list;
	}

    /**
     * 根据长度生成随机数字和字母的组合
     *  例：getStringRandom(8) 结果：mP8V60yt
     *  @return 生成的结果
     */
    public static String getStringRandom(int length) {
        // 随机生的值
        String val = "";
        Random random = new Random();
        for(int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                // 输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }
}
